/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.edu.ifrs.poa.control;

import br.edu.ifrs.poa.dao.NoticiaDAO;
import br.edu.ifrs.poa.model.Noticia;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author karenborges
 */
public class LeitorProcessor extends Processor {

    private void todasNoticias(HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        try {
            ArrayList noticias = NoticiaDAO.getNoticias();
            if (!noticias.isEmpty()) {
                Iterator it = noticias.iterator();
                while (it.hasNext()) {
                    Noticia news = (Noticia) it.next();
                    out.println("<h2>" + news.getTitulo() + " </h2>");
                    out.println("<p>" + news.getDescricao() + " </p>");
                    out.println("<p> Maiores detalhes em:" + news.getLink() + " </p>");
                    DateFormat df = DateFormat.getInstance();
                    out.println("<p> Publicada em:" + df.format(news.getData()) + " por " + news.getUsuario().getNome() + "</p>");
                    out.println("<hr>");
                }
                out.println("<a href=\"menu.jsp\"> Voltar ao menu </a>");
                out.println("</body>");
                out.println("</html>");
                out.close();
            }
        } catch (SQLException ex) {
            throw new ServletException(ex);
        } catch (ClassNotFoundException ex) {
            throw new ServletException(ex);
        }
    }

    private void noticiasPorData(HttpServletResponse response, String data) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        //TODO fazer a pesquisa por data
        out.println("<h2>... a partir de " + data + " </h2>");
        out.println("<a href=\"menu.jsp\"> Voltar ao menu </a>");
        out.println("</body>");
        out.println("</html>");
        out.close();
    }

    @Override
    public void execute() throws ServletException, IOException {
        getResponse().setContentType("text/html;charset=UTF-8");
        PrintWriter out = getResponse().getWriter();
        String identifica = (String) getContext().getAttribute("identifica");
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Sistema de Publicação de Noticias</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>" + identifica + ", estas são as noticias ... </h1>");
        String opcao = getRequest().getParameter("opcao");
        int op = Integer.parseInt(opcao);
        switch (op) {
            case 1:
                todasNoticias(getResponse());
                break;
            case 2:
                String data = getRequest().getParameter("data");
                noticiasPorData(getResponse(), data);
                break;
            case 3:
                getResponse().sendRedirect("menu.jsp");
                break;
        }
    }
}
